Namespace PL.MESSDK
    Public Interface ILot_PL
        Function ReleaseDetails(ByVal dataRow As DataRow) As Boolean
        Function ConfirmDetails(ByVal dataRow As DataRow) As Boolean
        Function ReserveDetails(ByVal dataRow As DataRow) As Boolean
        Function ReleaseHeader(ByVal dataRow As DataRow) As Boolean
    End Interface

    Public Class Lot_PL
        Inherits PLBase
        Implements IPLProcess, ILot_PL

        Public Sub New(ByVal queryName As String)
            MyBase.New(queryName)
        End Sub

        Public Sub New()
            MyBase.New()
        End Sub

        Sub OnRowUpdating(ByVal sender As Object, ByVal e As SqlRowUpdatingEventArgs) Handles MetaDataTable.OnRowUpdating

        End Sub

        Public Overrides Function Delete() As Boolean
            'Throw New NotImplementedException("Delete To Come...")
            Return False
        End Function

        Public Overrides Function Validate() As Boolean
            'Throw New NotImplementedException("Validate To Come...")
            Return Me.MetaDataTable.HasErrors
        End Function

        ''' <summary>
        ''' Validate a Lot DataRow
        ''' </summary>
        ''' <param name="row"></param>
        ''' <remarks>Errors are saved with row.SetColumnError method</remarks>
        Public Overrides Sub ValidateRow(row As DataRow)
            row.ClearErrors()
            'Mandatory Fields
            'TODO: Cache MetaString in AppManager, so here we read it once maybe for nothing
            'Dim ms As MetaString
            'ms = MetaString.GetMetaStringByCode("Mandatory", AppManager.ApplicationOption.LanguageId)

            'If IsDBNull(row("LotCD")) Then
            '    row.SetColumnError("LotCD", "Mandatory")
            'End If
            If IsDBNull(row("PrdID_FK")) Then
                row.SetColumnError("PrdID_FK", "Mandatory")
            End If
            If IsDBNull(row("LotTypeID_FK")) Then
                row.SetColumnError("LotTypeID_FK", "Mandatory")
            End If
            If IsDBNull(row("WhouseID_FK")) Then
                row.SetColumnError("WhouseID_FK", "Mandatory")
            End If
            If IsDBNull(row("LotDescr_Prm")) Then
                row.SetColumnError("LotDescr_Prm", "Mandatory")
            End If
            'If IsDBNull(row("LotDescr_Alt")) Then
            '    row.SetColumnError("LotDescr_Alt", "Mandatory")
            'End If
            If IsDBNull(row("LotQtyAvail")) Then
                row.SetColumnError("LotQtyAvail", "Mandatory")
            End If
            If IsDBNull(row("UnitID_FK")) Then
                row.SetColumnError("UnitID_FK", "Mandatory")
            End If

            If IsDBNull(row("RequestedQty")) Then
                row.SetColumnError("RequestedQty", "Mandatory")
            End If

            If IsDBNull(row("ProductionUnitId_FK")) Then
                row.SetColumnError("ProductionUnitId_FK", "Mandatory")
            End If

            If IsDBNull(row("RefDocCd")) Then
                row.SetColumnError("RefDocCd", "Mandatory")
            End If
            If IsDBNull(row("RefDocNo")) Then
                row.SetColumnError("RefDocNo", "Mandatory")
            End If
            '
            If row.HasErrors Then
                row.RowError = "Please check your columns, a validation error occur"
            End If
        End Sub

        Public Function BatchRecords(dataRow() As System.Data.DataRow) As Boolean Implements IPLProcess.BatchRecords
            Return True
        End Function

        Public Function CurrentRecord(dataRow As System.Data.DataRow) As Boolean Implements IPLProcess.CurrentRecord
            Return True
        End Function

        Public Function NewRecord(dataRow As System.Data.DataRow) As Boolean Implements IPLProcess.NewRecord
            dataRow("LotID") = Guid.NewGuid()

            dataRow("StatusID_FK") = IdRepository.APP_LOTSTATUS_PL

            dataRow("StartDate") = Date.Today()
            dataRow("RequiredDate") = Date.Today()
            dataRow("ManufacturedDate") = Date.Today()

            dataRow("LotQtyAvail") = 0

            dataRow("RefDocCd") = String.Empty
            dataRow("RefDocNo") = String.Empty


            Return True
        End Function

        ''' <summary>
        ''' Return a Lot DataRow
        ''' </summary>
        ''' <param name="lotID"></param>
        ''' <returns>Return a DataRow when found, Default = Noting</returns>
        ''' <remarks></remarks>
        Public Shared Function GetLotByID(ByVal lotID As Guid) As DataRow
            Dim aRow As DataRow = MetaQuery.GetDataById("Lot", lotID)
            Return aRow
        End Function
        '
        ''' <summary>
        ''' Return a LotStock DataRow
        ''' </summary>
        ''' <param name="lotID"></param>
        ''' <param name="siteSectionID"></param>
        ''' <returns>Return a DataRow when found, Default = Noting</returns>
        ''' <remarks></remarks>
        ''' <example> This sample shows how to call the GetLotSectionByID function.
        ''' <code>
        '''    Dim aSiteSectionRow As DataRow = Nothing
        '''    aSiteSectionRow = Lot_PL.GetLotSectionByID(CType(row("LotID_FK"), Guid), CType(row("FROMWhBinID_FK"), Guid))
        '''    If aSiteSectionRow Is Nothing Then
        '''        row.SetColumnError("FROMWhBinID_FK", "Invalide Site section for this Lot")
        '''    End If
        ''' </code>
        ''' </example>
        Public Shared Function GetLotSectionByID(ByVal lotID As Guid, siteSectionID As Guid) As DataRow
            Dim aRow As DataRow = Nothing
            Using lt As MetaQuery = MetaQuery.GetMetaQueryByName("LotStock")
                lt.SQLWhere = String.Format("LotID_FK = '{0}' AND WhBinID_FK = '{1}'", lotID.ToString, siteSectionID.ToString)
                lt.FillResultQuery()
                If lt.ResultQuery.Rows.Count > 0 Then
                    aRow = lt.ResultQuery.Rows(0)
                End If
                Return aRow
            End Using
        End Function



        Public Overridable Function ConfirmDetails(dataRow As System.Data.DataRow) As Boolean Implements ILot_PL.ConfirmDetails
            Dim sqlcmd As System.Data.SqlClient.SqlCommand = Nothing
            Try
                sqlcmd = MetaQuery.GetSPCmdByMetaName("LotConfirmDetail")
                '
                sqlcmd.Parameters("@LotID").Value = dataRow("LotID")
                sqlcmd.Parameters("@RecordVersion").Value = dataRow("RecordVersion")
                '
                sqlcmd.ExecuteNonQuery()
                Return True
            Catch sqlex As System.Data.SqlClient.SqlException
                Throw sqlex
            Catch ex As Exception
                Throw ex
            Finally
                If sqlcmd IsNot Nothing Then
                    sqlcmd.Connection.Close()
                    sqlcmd.Dispose()
                End If
            End Try
        End Function

        Public Overridable Function ReleaseDetails(dataRow As System.Data.DataRow) As Boolean Implements ILot_PL.ReleaseDetails
            Dim sqlcmd As System.Data.SqlClient.SqlCommand = Nothing
            Try
                sqlcmd = MetaQuery.GetSPCmdByMetaName("LotReleaseDetail")
                '
                sqlcmd.Parameters("@LotID").Value = dataRow("LotID")
                sqlcmd.Parameters("@RecordVersion").Value = dataRow("RecordVersion")
                '
                sqlcmd.ExecuteNonQuery()
                Return True
            Catch sqlex As System.Data.SqlClient.SqlException
                Throw sqlex
            Catch ex As Exception
                Throw ex
            Finally
                If sqlcmd IsNot Nothing Then
                    sqlcmd.Connection.Close()
                    sqlcmd.Dispose()
                End If
            End Try
        End Function

        Public Overridable Function ReleaseHeader(dataRow As System.Data.DataRow) As Boolean Implements ILot_PL.ReleaseHeader
            Dim sqlcmd As System.Data.SqlClient.SqlCommand = Nothing
            'Validating if all details LotTran has been Released
            'If not all Released Abort Release header.
            'Releasing header is a RECPT Lotran type.

            Try
                sqlcmd = MetaQuery.GetSPCmdByMetaName("<proceesbatch metaquery>")
                'Repete line for all parameters needed
                sqlcmd.Parameters("@Param1").Value = dataRow(0)  ' No number please use columname

                sqlcmd.ExecuteReader(CommandBehavior.CloseConnection)
                Return True
            Catch sqlex As System.Data.SqlClient.SqlException
                Throw sqlex
            Finally
                If sqlcmd IsNot Nothing Then
                    sqlcmd.Connection.Close()
                    sqlcmd.Dispose()
                End If
            End Try
        End Function

        Public Overridable Function ReserveDetails(dataRow As System.Data.DataRow) As Boolean Implements ILot_PL.ReserveDetails
            Dim sqlcmd As System.Data.SqlClient.SqlCommand = Nothing
            Try
                sqlcmd = MetaQuery.GetSPCmdByMetaName("LotReserveDetail")
                '
                sqlcmd.Parameters("@LotID").Value = dataRow("LotID")
                sqlcmd.Parameters("@RecordVersion").Value = dataRow("RecordVersion")
                '
                sqlcmd.ExecuteNonQuery()
                Return True
            Catch sqlex As System.Data.SqlClient.SqlException
                Throw sqlex
            Catch ex As Exception
                Throw ex
            Finally
                If sqlcmd IsNot Nothing Then
                    sqlcmd.Connection.Close()
                    sqlcmd.Dispose()
                End If
            End Try
        End Function
    End Class

End Namespace
